package com;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;

/**
 * 1、整型二维数组一系列简单求和实现
 */
public class ArrayShow {
    public static void main(String[] args) {
        Random random = new Random();
        int[][] arr = new int[16][16];
        int[] sum1 = new int[16];
        int[] sum2 = new int[16];
        int sum3 = 0,sum4 = 0;
        for (int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++){
                //随机生成1~100的值
                arr[i][j] = random.nextInt(100)+1;
                //i代表列数，j代表列数，列数固定使求和为每行和，行数固定求和为列数和
                sum1[i]+=arr[i][j];
                sum2[j]+=arr[i][j];
                //左上到右下元素行数和列数相同
                if(i==j)
                    sum3+=arr[i][j];
                //下标和从0开始，右上到左下元素行数和列数和为总元素个数-1
                if(i+j==15)
                    sum4+=arr[i][j];
            }
        }
        //方便检验结果，输出数组中随机的所有元素
        for (int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
        System.out.println("每行元素和为" + Arrays.toString(sum1));
        System.out.println("每列元素和为" + Arrays.toString(sum2));
        System.out.println("左上到右下元素和为"+sum3);
        System.out.println("右上到左下元素和为"+sum4);
    }
}
